//
//  AddViewController.m
//  Address-book
//
//  Created by 任伟 on 16/1/3.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "AddViewController.h"

@interface AddViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *numberField;
@property (weak, nonatomic) IBOutlet UIButton *addButton;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 监听文本框
    [self.nameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [self.numberField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];

    [self.addButton addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 键盘称为第一响应者
    [self.nameField becomeFirstResponder];
    
}

/// 添加联系人
- (void)addClick {
    
    // 判断代理方法是否能够响应
    if ([self.delegate respondsToSelector:@selector(addViewController:withContactModel:)]) {
        
        ContactModel *model = [[ContactModel alloc] init];
        model.name = self.nameField.text;
        model.number = self.numberField.text;
        
        [self.delegate addViewController:self withContactModel:model];
    }
    
    // 回到上一个页面
    [self.navigationController popViewControllerAnimated:YES];
}

// 文本框值改变的时候调用
- (void)textChange {

    self.addButton.enabled = self.nameField.text.length && self.numberField.text.length;
}

@end
